package com.dongdongshop.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.constant.ContentRedis;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.content.TbContent;
import com.dongdongshop.pojo.content.TbContentExample;
import com.dongdongshop.service.ContentServiceI;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@Service
public class ContentServiceImpl implements ContentServiceI {

    @Resource
    private TbContentMapper  contentMapper;
    @Resource
    private RedisTemplate redisTemplate;

    //前台轮播图
    @Override
    public List<TbContent> getCarousel() {
        HashOperations hashJedis = redisTemplate.opsForHash();
        //如果redis中存在
        if (hashJedis.hasKey(ContentRedis.CAROUSEL,ContentRedis.FIELD)){
            //在缓存中取出数据
            List<TbContent> list= (List<TbContent>) hashJedis.get(ContentRedis.CAROUSEL,ContentRedis.FIELD);
            return list;
        }else {//redis中没有数据
            //查询数据库对应的数据，加载到redis中
            List<TbContent> list=  contentMapper.getCarousel();
            //将得到的数据放到redis中
            hashJedis.put(ContentRedis.CAROUSEL,ContentRedis.FIELD,list);
            ///设置有效时间。因为在springboot里村Redis数据时会将数据的key自动帮我们序列化。无法在可视化工具里删除
            redisTemplate.expire(ContentRedis.CAROUSEL,1, TimeUnit.HOURS);
            return list;
        }
    }

    //前台今日推荐
    @Override
    public List<TbContent> findRecommendAll() {
        HashOperations hashJedis = redisTemplate.opsForHash();
        if (hashJedis.hasKey(ContentRedis.RECOMMEND,ContentRedis.FIELD)){
            List<TbContent> list=(List<TbContent>) hashJedis.get(ContentRedis.RECOMMEND,ContentRedis.FIELD);
            return list;
        }else {
            List<TbContent> list=contentMapper.findRecommendAll();
            hashJedis.put(ContentRedis.RECOMMEND,ContentRedis.FIELD,list);
            redisTemplate.expire(ContentRedis.RECOMMEND,1,TimeUnit.MINUTES);
            return list;
        }
    }
    //前台猜你喜欢
    @Override
    public List<TbContent> findLikeAll() {
        HashOperations hashJedis = redisTemplate.opsForHash();
        if (hashJedis.hasKey(ContentRedis.LIKE,ContentRedis.FIELD)){
            List<TbContent> list=(List<TbContent>) hashJedis.get(ContentRedis.LIKE,ContentRedis.FIELD);
            return list;
        }else {
            List<TbContent> list=contentMapper.findLikeAll();
            hashJedis.put(ContentRedis.LIKE,ContentRedis.FIELD,list);
            redisTemplate.expire(ContentRedis.LIKE,1,TimeUnit.MINUTES);
            return list;
        }
    }

    @Override
    public List<TbContent> findAmusingAll() {
        HashOperations hashJedis = redisTemplate.opsForHash();
        if (hashJedis.hasKey(ContentRedis.AMUSING,ContentRedis.FIELD)){
            List<TbContent> list= (List<TbContent>) hashJedis.get(ContentRedis.AMUSING,ContentRedis.FIELD);
            return list;
        }else {
            List<TbContent> list= contentMapper.findAmusingAll();
            hashJedis.put(ContentRedis.AMUSING,ContentRedis.FIELD,list);
            redisTemplate.expire(ContentRedis.AMUSING,1,TimeUnit.MINUTES);
            return list;
        }
    }



    @Override
    public void deleteContent(Long[] ids) {
        contentMapper.deleteContent(ids);
    }

    @Override
    public void updateContent(TbContent content) {
        contentMapper.updateByPrimaryKeySelective(content);
    }

    @Override
    public TbContent getContent(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insertContent(TbContent content) {
        contentMapper.insert(content);
    }

    @Override
    public List<TbContent> listContent() {
        return contentMapper.selectByExample(new TbContentExample());
    }
}
